<template>
  <div>
    <!--    <button @click = "flash"></button>-->

    <el-row type="flex">
      <el-col></el-col>
      <el-col
        ><el-button type="primary"
                    @click="dialogFormVisible = true"
          >新增试题</el-button
        >
      </el-col>
    </el-row>

    <div v-for="(item, index) in questionList" :key="index">
      <qsItem
        :qsItem="item"
        class="text"
        :qs-item-index="index"
        @deleteSign="deleteQs"
      ></qsItem>
    </div>

<!--    <div class="block">-->
<!--      <span class="demonstration">完整功能</span>-->
<!--      <el-pagination-->
<!--        @size-change="handleSizeChange"-->
<!--        @current-change="handleCurrentChange"-->
<!--        :current-page="currentPage4"-->
<!--        :page-sizes="[5, 10, 15, 20]"-->
<!--        :page-size="5"-->
<!--        layout="total, sizes, prev, pager, next, jumper"-->
<!--      >-->
<!--      </el-pagination>-->
<!--    </div>-->
    <el-dialog
        :visible.sync="dialogFormVisible">
      <questionAdd
          @cancel="dialogFormVisible = false"
          @addQuestion="addQuestion"></questionAdd>
    </el-dialog>

  </div>
</template>

<script>
import axiosinstance from "@/util/http";
import questionAdd from '@/components/Question/questionAdd'
import qsItem from "@/view/Home/components/QuestionBase/qsItem";

export default {
  name: "QuestionBaseDetail",
  prpos: [],
  data() {
    return {
      qb_id: null,
      user: null,
      questionList: [],
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      number: null,
      dialogFormVisible:false,
      qsform: {
        type: "",
        title: "",
        qs_body: {
          answers: "",
          selections: [],
        },
      },

    };
  },

  computed: {
    totalprice: function () {
      return this.questionList.length;
    },
  },

  methods: {
    // deleteSign(){
    //   this.$emit('deleteSign',this.qsItem);
    // },

    getQuestions(username, qb_id, page, pagesize) {
      axiosinstance
        .get("/questionbase/questionlist", {
          params: {
            username: username,
            qb_id: qb_id,
            page: page,
            pagesize: pagesize,
          },
        })
        .then((res) => {
          //console.log(res);
          this.questionList = res.data.data;
          console.log("detail", this.questionList);
        });
    },

    handleSizeChange(val) {
      //  this.pagesize = size;
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      // this.currentPage = currentPage;
      console.log(`当前页: ${val}`);
    },

    async deleteQs(index) {
      console.log(index, "index");
      this.questionList.splice(index.index, 0);
      let res = await axiosinstance({
        url: '/questionbase/delquestion',
        method: 'post',
        data:{
          qb_id: this.$route.query.qb_id,
          qt_ids: index.item.id,
          username: this.$store.getters.getuser,
        }
      });
      if(res){
        this.$message({
          showClose: true,
          message: '删除成功',
          type: 'success'
        });
      }else{
        this.$message({
          showClose: true,
          message: '删除失败',
          type: 'err'
        });
      }
    },
    deleteQsRequest() {},

    addQuestion(question){

      axiosinstance.post('/questionbase/addquestion',{
        questionArray:[question],
        QBid:this.qb_id,
        username:this.$store.getters.getuser.username
      }).then(res =>{
         console.log("res",res)
        if(res.data.status===0 && res.data.successDataNum>0){
          this.$message({
            message:"添加题目成功",
            type:"success",
          })
          this.dialogFormVisible=false
          this.$router.go(0);

        }
      })

      console.log("question",question);
    }
  },

  created() {
    // this.qsItemlist();
    this.qb_id = this.$route.query.qb_id;
    this.user = this.$store.getters.getuser;
    axiosinstance
      .get("/questionbase/questionlist", {
        params: {
          username: this.user.username,
          qb_id: this.qb_id,
          page: 1,
          pagesize: 20,
        },
      })
      .then((res) => {
        //console.log(res);
        this.questionList = res.data.data;
        console.log("detail", this.questionList);
      });
  },
  components: {
    qsItem,
    questionAdd,
  },
};
</script>

<style scoped lang="less">

</style>